<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\User;
use Illuminate\Http\Request;
use App\Models\Authorization;
use Illuminate\Support\Facades\Cache;
use App\Transformers\AuthorizationTransformer;

class AuthController extends BaseController
{
    /**
     * @api {post} /authorizations 创建一个token (create a token) 即登录
     * @apiDescription 创建一个token (create a token)
     * @apiGroup Auth
     * @apiPermission none
     * @apiParam {Phone} phone     手机
     * @apiParam {String} captcha  验证码
     * @apiVersion 0.2.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 201 Created
     *     {
     *         "data": {
     *             "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOi8vbHVtZW4tYXBpLWRlbW8uZGV1L2FwaS9hdXRob3JpemF0aW9ucyIsImlhdCI6MTQ4Mzk3NTY5MywiZXhwIjoxNDg5MTU5NjkzLCJuYmYiOjE0ODM5NzU2OTMsImp0aSI6ImViNzAwZDM1MGIxNzM5Y2E5ZjhhNDk4NGMzODcxMWZjIiwic3ViIjo1M30.hdny6T031vVmyWlmnd2aUr4IVM9rm2Wchxg5RX_SDpM",
     *             "expired_at": "2017-03-10 15:28:13",
     *             "refresh_expired_at": "2017-01-23 15:28:13"
     *         }
     *     }
     * @apiErrorExample {json} Error-Response:
     *     HTTP/1.1 401
     *     {
     *       "error": "验证码错误"
     *     }
     */
    public function store(Request $request)
    {
        // $validator = \Validator::make($request->input(), [
        //     'phone' => 'required|phone|unique:cut_users',
        //     'code' => 'required|string',
        // ]);
        // if ($validator->fails()) {
        //     return $this->returnError($validator);
        // }
        $code = $request->get('code');
        $phone = $request->get('phone');
        $country = $request->get('country');
        
		$from = $request->get('dx');
		$pre = $from ? $from .'_' : '';
        //check code 
        $value = Cache::get($pre.'phone_captcha_'.$country.'_'.$phone);
        // $value = 1234;
        // if(empty($value)){
        // 	return $this->returnFail(trans('auth.captcha_retry_again'), $code);
        // }
        // if($value != $code){
        // 	return $this->returnFail(trans('auth.captcha_noeq'));
        // }
		// if (\Auth::check()) {
		    // 用户已登录...
		// }
		$exists = User::where('phone', $phone)->first();
		$password = '123456';
		if(!$exists){
			
			$attributes = [
				'name' => $phone,
				'phone' => $phone,
				'password' => app('hash')->make($password),
			];
			$user = User::create($attributes);
			if(!$user){
				// return $this->returnFail(trans('auth.nocorrect_user'));
			}
		}
		//tymon/jwt-auth/src/JWTAuth.php 会返回token
		if(! $token = \Auth::guard('api')->attempt(['phone' => $phone, 'password' => $password])){
			// $this->response->errorUnauthorized(trans('auth.incorrect'));
			return $this->returnFail(trans('auth.auth_token_empty'));
		}
		
        $authorization = new Authorization($token);
		$item = $authorization->toArray();
		
		return $this->returnSuccess($item, trans('auth.correct'));
    }

    /**
     * @api {put} /authorizations/current 刷新token(refresh token)
     * @apiDescription 刷新token(refresh token)
     * @apiGroup Auth
     * @apiPermission JWT
     * @apiVersion 0.2.0
     * @apiHeader {String} Authorization 用户旧的jwt-token, value已Bearer开头
     * @apiHeaderExample {json} Header-Example:
     *     {
     *       "Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6XC9cL21vYmlsZS5kZWZhcmEuY29tXC9hdXRoXC90b2tlbiIsImlhdCI6IjE0NDU0MjY0MTAiLCJleHAiOiIxNDQ1NjQyNDIxIiwibmJmIjoiMTQ0NTQyNjQyMSIsImp0aSI6Ijk3OTRjMTljYTk1NTdkNDQyYzBiMzk0ZjI2N2QzMTMxIn0.9UPMTxo3_PudxTWldsf4ag0PHq1rK8yO9e5vqdwRZLY"
     *     }
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *         "data": {
     *             "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOi8vbHVtZW4tYXBpLWRlbW8uZGV1L2FwaS9hdXRob3JpemF0aW9ucyIsImlhdCI6MTQ4Mzk3NTY5MywiZXhwIjoxNDg5MTU5NjkzLCJuYmYiOjE0ODM5NzU2OTMsImp0aSI6ImViNzAwZDM1MGIxNzM5Y2E5ZjhhNDk4NGMzODcxMWZjIiwic3ViIjo1M30.hdny6T031vVmyWlmnd2aUr4IVM9rm2Wchxg5RX_SDpM",
     *             "expired_at": "2017-03-10 15:28:13",
     *             "refresh_expired_at": "2017-01-23 15:28:13"
     *         }
     *     }
     */
    public function update()
    {
        $authorization = new Authorization(\Auth::refresh());
		$item = $authorization->toArray();
		
		return $this->returnSuccess($item, trans('auth.correct'));
        // return $this->response->item($authorization, new AuthorizationTransformer());
    }

    /**
     * @api {delete} /authorizations/current 删除当前token (delete current token)
     * @apiDescription 删除当前token (delete current token)
     * @apiGroup Auth
     * @apiPermission jwt
     * @apiVersion 0.1.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 204 No Content
     */
    public function destroy()
    {
        \Auth::logout();

        return $this->response->noContent();
    }
}
